%clear all; clc;close all;
%Updated: 03/29/217

%Compute impulse-response functions using finer endowment and debt grids
% #endowment gridpoints (na2)=500, #debt gridpoints (nb2)=1500
% Initial endowment level: uncoditional mean
% Feed in one-std-deviation shock in period 1

lambda = 0.184;
rf     = 0.02;

p   =load('irfp.txt');
h   =load('irfh.txt');
b   =load('irfb.txt');
ct  =load('irfct.txt');
r   =load('irfr.txt');
%m   =load('irfm.txt');
gn  =load('irfgn.txt');
tau =load('irftau.txt');
a   =load('irfa.txt');
cn  =load('irfcn.txt');
ww  =load('irfww.txt');
v   =load('irfv.txt');
bor =load('irfbor.txt');

p0   =p(:,1);
h0   =h(:,1);
b0   =b(:,1);
ct0  =ct(:,1);
r0   =r(:,1);
gn0  =gn(:,1);
tau0 =tau(:,1);
a0   =a(:,1);
cn0  =cn(:,1);
v0   =v(:,1);
ww0  =ww(:,1);
bor0 =bor(:,1);


b   = -b;   %/(lambda+rf);
b0  = -b0; %/(lambda+rf);
% r   = 100*r;
% r0  = 100*r0;
% r05 = 100*r05;
% r15 = 100*r15;

nirf = 15;      %number of periods
time = 1:nirf;

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray = {'--','-','-.';[0.23137255012989 0.443137258291245 0.337254911661148],[0.078  0.1686  0.549],[1  0.4  0.4];1.25,1.25,1.25}';

endt=10;

%if (max(bor(:,1))>0);
%    disp('Differences in access/borrowing paths in case 1');
%elseif (max(bor0(:,1))>0);
%    disp('Differences in access/borrowing paths in case 2');
%elseif (max(bor05(:,1))>0);
%    disp('Differences in access/borrowing paths in case 3');
%elseif (max(bor15(:,1))>0);
%    disp('Differences in access/borrowing paths in case 4');
%end;

NameArray = {'LineStyle','Color','LineWidth'};
ValueArray2 = {':','-';[0.078  0.1686  0.549],[1  0.4  0.4];1.25,1.25}';

ValueArray0 = {'-';[1  0.4  0.4];1.25}';


figure;
subplot(4,2,1);P=plot(time,a0(1:nirf,1));title('y^T','Fontweight','normal');
AX1 = gca;box on;
set(AX1,'YTick',-0.01:0.01:0.01);
%axis(AX1, [0 time(endt) -0.01 0.01]);
set(P,NameArray,ValueArray0);
subplot(4,2,2);P=plot(time,100*(-h0(1:nirf,1)));title('unemployment','Fontweight','normal');
%lgd=legend('low debt', 'intermediate debt','high debt');lgd.Location='SouthEast';
%legend('low debt (0.75b^{ss})','high debt (1.25b^{ss})','Location','NorthEast');
ylabel('%');
set(P,NameArray,ValueArray0);
AX1 = gca;box on;
set(AX1,'YTick',-1:1:1);
axis(AX1, [0 time(endt) -1 1]);
subplot(4,2,3);P=plot(time,p0(1:nirf,1));title('p^N','Fontweight','normal');
AX1 = gca;box on;
set(AX1,'YTick',-0.01:0.01:0.01);
%axis(AX1, [0 time(endt) -0.01 0.01]);
set(P,NameArray,ValueArray0);
subplot(4,2,4);P=plot(time,gn0(1:nirf,1));title('g^N','Fontweight','normal');
AX1 = gca;box on;
set(AX1,'YTick',-0.005:0.005:0.005);
%axis(AX1, [0 time(endt) -0.005 0.005]);
set(P,NameArray,ValueArray0);
subplot(4,2,5);P=plot(time,b0(1:nirf,1));title('debt','Fontweight','normal');
set(P,NameArray,ValueArray0);
AX1 = gca;box on;
set(AX1,'YTick',-0.005:0.005:0.01);
%axis(AX1, [0 time(endt) -0.005 0.01]);
subplot(4,2,6);P=plot(time,100*r0(1:nirf,1));title('spreads','Fontweight','normal');ylabel('%');
set(P,NameArray,ValueArray0);
AX1 = gca;box on;
set(AX1,'YTick',-0.6:0.3:0);
%axis(AX1, [0 time(endt) -0.6 0.05]);
subplot(4,2,7);P=plot(time,ct0(1:nirf,1));title('c^T','Fontweight','normal');
set(P,NameArray,ValueArray0);
AX1 = gca;box on;
set(AX1,'YTick',-0.01:0.01:0.01);
%axis(AX1, [0 time(endt) -0.01 0.01]);
subplot(4,2,8);P=plot(time,v0(1:nirf,1));title('v','Fontweight','normal');
set(P,NameArray,ValueArray0);
AX1 = gca;box on;
set(AX1,'YTick',-0.01:0.005:0.005);
%axis(AX1, [0 time(endt) -0.01 0.005]);

dbstop;

figure;
subplot(4,2,1);P=plot(time,a(1:nirf,1),time,a(1:nirf,1),time,a(1:nirf,1));title('y^T','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,2);P=plot(time,-h0(1:nirf,1),time,-h05(1:nirf,1),time,-h15(1:nirf,1));title('unemployment','Fontweight','normal');
%lgd=legend('low debt', 'intermediate debt','high debt');lgd.Location='SouthEast';
legend('low debt', 'intermediate debt','high debt','Location','NorthEast');
set(P,NameArray,ValueArray);
subplot(4,2,3);P=plot(time,p0(1:nirf,1),time,p05(1:nirf,1),time,p15(1:nirf,1));title('p^N','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,4);P=plot(time,gn0(1:nirf,1),time,gn05(1:nirf,1),time,gn15(1:nirf,1));title('g^N','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,5);P=plot(time,b0(1:nirf,1),time,b05(1:nirf,1),time,b15(1:nirf,1));title('debt','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,6);P=plot(time,r0(1:nirf,1),time,r05(1:nirf,1),time,r15(1:nirf,1));title('spreads','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,7);P=plot(time,ct0(1:nirf,1),time,ct05(1:nirf,1),time,ct15(1:nirf,1));title('c^T','Fontweight','normal');
set(P,NameArray,ValueArray);
subplot(4,2,8);P=plot(time,cn0(1:nirf,1),time,cn05(1:nirf,1),time,cn15(1:nirf,1));title('c^N','Fontweight','normal');
set(P,NameArray,ValueArray);


